package com.lineage.data.item_etcitem.shop;

import com.lineage.data.event.CardSet;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.lock.CharItemsTimeReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemName;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import java.sql.Timestamp;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class VIP_Card_01 extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(VIP_Card_01.class);
  
  public static ItemExecutor get() {
    return new VIP_Card_01();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    try {
      if (item == null)
        return; 
      if (pc == null)
        return; 
      if (!CardSet.START) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("目前未开放VIP道具使用。"));
        return;
      } 
      if (item.get_card_use() == 2) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("这张加值卡片已经到期。"));
        return;
      } 
      if (item.get_card_use() == 1) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("这张卡片开启中,请注意使用期限。"));
        return;
      } 
      int[] itemids = { 44129, 44188, 44130, 44189 };
      int array[], length = (array = itemids).length;
      int i = 0;
      while (i < length) {
        int itemid = array[i];
        if (pc.getInventory().checkCardEquipped(itemid)) {
          L1Item temp = ItemTable.get().getTemplate(itemid);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("你已经启动了一个" + temp.getName()));
          return;
        } 
        i++;
      } 
      long time = System.currentTimeMillis();
      long x1 = (CardSet.USE_TIME * 60 * 60);
      long x2 = x1 * 1000L;
      long upTime = x2 + time;
      Timestamp ts = new Timestamp(upTime);
      item.set_time(ts);
      item.set_card_use(1);
      CharItemsTimeReading.get().addTime(item.getId(), ts);
      pc.sendPackets((ServerBasePacket)new S_ItemName(item));
      CardSet.set_card_mode(pc, item);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
